import React from 'react'
// 导入连接store的方法connect()
import { connect } from 'react-redux'

class Total extends React.Component {

  render () {
    console.log('Total:', this.props)
    const { total } = this.props
    return <div>商品总数：【{total}】</div>
  }
}

/**
 * 连接store
 * @param {*} state store的数据
 * @param {*} ownProps 被增强组件自身的props
 */
const mapStateToProps = (state, ownProps) => {
  console.log(state, ownProps)
  // 返回值是提供给组件props的数据
  return {
    total: Object.values(state).reduce((p, n) => p + n)
  }
}

// 使用高阶组件connect()增强total=》获取store的数据
const newTotal = connect(mapStateToProps)(Total)

export default newTotal
